package com.peng.leetcode.tree;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * MinDiffInBST
 *
 * @author: lupeng6
 * @create: 2021/1/12 20:29
 */
public class MinDiffInBST {

    public static void main(String[] args) {

    }

    private List<Integer> values = new ArrayList<>();

    public int minDiffInBST(TreeNode root) {
        inorder(root);
        Collections.sort(values);
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < values.size() - 1; i++) {
            min = Math.min(values.get(i + 1) - values.get(i), min);
        }
        return min;
    }

    private void inorder(TreeNode root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        values.add(root.val);
        inorder(root.right);
    }
}
